<?php
namespace Admin\Controller;

class ImageController extends AdminController
{
    public function index()
    {
        $image = M('image')->field(array('onethink_image.*', 'onethink_goods.title'))->join('onethink_goods on onethink_image.gid = onethink_goods.id')->select();
        $this->assign('res', $image);
        $this->meta_title = '商品图片管理';
        $this->display();
    }

    public function delete()
    {
        $id  = I('get.id');
        $res = M('Image')->where(array('id' => $id))->delete();
        if ($res) {
            $this->success('删除成功！');
        } else {
            $this->error(M('image')->getError());
        }
    }

    public function update()
    {
        if (IS_POST) {
            $info         = $this->upload();
            $arr = array();
            foreach($info as $v){
                $arr[] = $v['savepath'].$v['savename'];
            }
            $pic = implode(',',$arr);
            $_POST['pic'] = $pic;
            $id           = session('id');
            $image        = D('image');
            $res2         = $image->create(I('post.'));
            if ($res2) {
                $res1 = $image->where(array('id' => $id))->save();
                if ($res1) {
                	session('id',null);
                    $this->success('修改成功', U('index'));
                } else {
                    $this->error($image->getError());
                }
            } else {
                $this->error($image->getError());
            }

        } else {
            $id  = I('get.id');
            $res = M('image')->field(array('onethink_image.*', 'onethink_goods.title'))->join('onethink_goods on onethink_image.gid = onethink_goods.id where onethink_image.id = ' . $id)->select()[0];
            if ($res) {
                session('id', $id);
                $this->assign('res', $res);
            }
            $this->display();
        }
    }

    public function add()
    {
        if (IS_POST) {
        	$info         = $this->upload();
            $arr = array();
            foreach($info as $v){
                $arr[] = $v['savepath'].$v['savename'];
            }
            $pic = implode(',',$arr);
            $_POST['pic'] = $pic;
        	$image = D('image');
        	$res1 = $image->create(I('post.'));
        	if($res1){
        		if($image->add()){
        			$this->success('添加成功！',U('index'));
        		}else{
        			$this->error($image->getError());
        		}
        	}else{
        		$this->error($image->getError());
        	}
        } else {
        	$res = M('goods')->select();
        	$this->assign('res',$res);
            $this->display();
        }
    }

    public function upload()
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->savePath = '/Public/Uploads/'; // 设置附件上传目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功
            return $info;
        }
    }
}
